package cn.xfdzx.android.apps.shop.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.ShopCatoryListAdapter;
import cn.xfdzx.android.apps.shop.bean.ShopAddGoodTypeBeen;

/**
 * @autor: zf
 * @Description: 选择分类
 * @Date: 2020/8/20 12:18
 */
public class ChooseShopCatgoryDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private TextView tvCancel;
    private TextView tvOk;
    private List<ShopAddGoodTypeBeen.DataBean> catoryList;
    private setChoseTypeListener choseTypeListener;
    private ShopCatoryListAdapter shopCatoryListAdapter;
    private RecyclerView recycle_shopcatory;
    private int catoryId;
    private TextView tv_seltype;

    public ChooseShopCatgoryDialog(@NonNull Context activity,List<ShopAddGoodTypeBeen.DataBean> catoryList) {
        super(activity, R.style.CustomDialog_Translucent);
        this.context = activity;
        this.catoryList = catoryList;
        setCanceledOnTouchOutside(false); // 点击空白区域可以Dismiss对话框
        setCancelable(false); // 点击返回按键可以Dismiss对话框
        init();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        init();
    }

    public void setDate(String catoryTitle) {
        //tv_seltype.setText("切换分类会导致上一级分类无法保存请您选择"+catoryTitle+"的店铺分类");
        tv_seltype.setText("切换分类会导致上一级分类无法保存，请您选择对应的店铺分类");
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_chosestoretype, null);
        setContentView(view);

        tv_seltype = findViewById(R.id.tv_seltype);
        tvCancel = findViewById(R.id.tv_cancle);
        tvOk = findViewById(R.id.tv_ok);
        recycle_shopcatory = findViewById(R.id.recycle_shopcatory);
        recycle_shopcatory.setLayoutManager(new LinearLayoutManager(context));
        shopCatoryListAdapter = new ShopCatoryListAdapter(catoryList);
        recycle_shopcatory.setAdapter(shopCatoryListAdapter);
        catoryId = catoryList.get(0).getId();
        shopCatoryListAdapter.setItemCatoryClickListener(new ShopCatoryListAdapter.OnItemItemCatoryClickListener() {
            @Override
            public void onItemItemCatoryClick(int position, List<ShopAddGoodTypeBeen.DataBean> catoryDate) {
                catoryId = catoryDate.get(position).getId();
                catoryList = catoryDate;
                shopCatoryListAdapter.refreshDatas(catoryDate);
            }
        });


        tvCancel.setOnClickListener(this);
        tvOk.setOnClickListener(this);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 高度设置为屏幕的0.6
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_cancle:
                this.dismiss();
                break;
            case R.id.tv_ok:
                choseTypeListener.onChoseType(catoryId+"");
                break;
            default:
                this.dismiss();
                break;

        }
        this.dismiss();
    }
    public void setChoseTypeListener(setChoseTypeListener listener){
            this.choseTypeListener = listener;
    }

    public interface setChoseTypeListener {
        void onChoseType(String catoryId);
    }
}
